<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>AI</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 0;
            padding: 0;
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
            height: 100vh;
            background-color: white;
            
        }
        .chat-container {
            height: 100%;
            width: 90%;
            max-width: 600px;
            background: white;
            padding: 20px;
            border-radius: 10px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
            background-color: #f7f9fc;
        }
        .chat-box {
            height: 70%;
            overflow-y: auto;
            border: 1px solid #ddd;
            padding: 10px;
            border-radius: 5px;
            background: #fafafa;
        }
        .input-container {
            display: flex;
            margin-top: 10px;
            margin-bottom: 10px;
            justify-content: center;
            align-items: center;
        }

        #preset-select {
            padding: 10px 15px;
            font-size: 16px;
            border-radius: 8px;
            border: 1px solid #ccc;
            background-color: #f8f8f8;
            transition: all 0.3s ease;
            width: 200px;
        }

        #preset-select option {
            padding: 10px;
        }
        
        input {
            flex: 1;
            padding: 10px;
            border: 1px solid #ccc;
            border-radius: 5px;
        }
        button {
            padding: 10px 15px;
            border: none;
            background: #007bff;
            color: white;
            cursor: pointer;
            border-radius: 5px;
            margin-left: 5px;
            transition: background-color 0.3s;
        }
        button:hover {
            background: #0056b3;
        }
        .message {
            padding: 5px 10px;
            margin: 5px 0;
            border-radius: 5px;
            max-width: 80%;
        }
        .user-message {
            background: #007bff;
            color: white;
            align-self: flex-end;
        }
        .ai-message {
            background: #e9ecef;
            color: black;
            align-self: flex-start;
        }
    </style>
</head>
<body>

    <div class="chat-container">
        <h2 style="text-align: center; color: #555657;">模型助手</h2>

        <div class="input-container">
            <select id="preset-select">
                <option value="preset_prompt_1">问题分配</option>
                <option value="preset_prompt_2">参数提示</option>
            </select>
        </div>

        <div id="chat-box" class="chat-box"></div>

        <div class="input-container">
            <input type="text" id="message" placeholder="输入你的问题..." onkeydown="handleKey(event)">
            <button onclick="sendMessage()">发送</button>
        </div>
    </div>

        <script>
        function handleKey(event) {
            if (event.key === "Enter") {
                sendMessage();
            }
        }

        function sendMessage() {
            const messageInput = document.getElementById("message");
            const selectedPreset = document.getElementById("preset-select").value;
            const message = messageInput.value.trim();
            if (!message) return;

            const chatBox = document.getElementById("chat-box");

            // 用户输入显示
            const userMessageDiv = document.createElement("div");
            userMessageDiv.className = "message user-message";
            userMessageDiv.textContent = message;
            chatBox.appendChild(userMessageDiv);
            chatBox.scrollTop = chatBox.scrollHeight;

            messageInput.value = "";  // 清空输入框

            // AI 响应区域
            const aiMessageDiv = document.createElement("div");
            aiMessageDiv.className = "message ai-message";
            chatBox.appendChild(aiMessageDiv);

            fetch("/ai/talk", {
                method: "POST",
                headers: { "Content-Type": "application/json" },
                body: JSON.stringify({ message, preset: selectedPreset })
            }).then(response => {
                const reader = response.body.getReader();
                const decoder = new TextDecoder();
                aiMessageDiv.textContent = "";  // 先清空 AI 文字

                function readStream() {
                    reader.read().then(({ done, value }) => {
                        if (done)
                        {
                            // 假设 AI 返回的是纯文本关键词，例如 "库存管理"
                            const finalCategory = aiMessageDiv.textContent.trim();
                            window.parent.postMessage({ category: finalCategory }, "*");
                            return;
                        }
                        aiMessageDiv.textContent += decoder.decode(value, { stream: true });
                        chatBox.scrollTop = chatBox.scrollHeight;  // 保持滚动
                        readStream();
                    });
                }
                readStream();

            }).catch(error => {
                console.error("请求失败:", error);
                aiMessageDiv.textContent = "⚠️ AI 服务器异常，请稍后重试。";
            });
        }
    </script>

</body>
</html>
